<?php
// +----------------------------------------------------------------------
// | Action ：报名模型
// +----------------------------------------------------------------------
// | DateTime：2015年12月8日
// +----------------------------------------------------------------------
// | Author: Danson <zoudan916@163.com>
// +----------------------------------------------------------------------

namespace Home\Model;

use Think\Model;

class VoetModel extends Model{

    /* 指点数据库名 */
    protected $tableName = 'lord_voet';

    /* 浏览记录 */
    public function save_num($lord_id){
        $result['lord_id'] = $lord_id;
        $result['openid'] = $_SESSION['wx_user']['openid'];
        $info = $this->where(array($result))->find();
        if(empty($info)){
            $result['nickname'] = $_SESSION['wx_user']['nickname'];
            $result['headimgurl'] = $_SESSION['wx_user']['headimgurl'];
            $this->add($result);
            M('lord_user')->where(array('lord_id'=>$lord_id))->setInc('scan_num',1);
            M('lord_user')->where(array('lord_id'=>$lord_id))->setInc('fans_num',1);
        }else{
            $this->where($result)->setInc('scan_num',1);
            M('lord_user')->where(array('lord_id'=>$lord_id))->setInc('scan_num',1);
        }
    }

    /* 投票记录 */
    public function save_voet($lord_id){
        $result['lord_id'] = $lord_id;
        $result['openid'] = $_SESSION['wx_user']['openid'];
        $all['lord_id'] = $lord_id;
        $all['openid'] = $_SESSION['wx_user']['openid'];
        $info = $this->where(array($result))->find();
        if(empty($info)){
            $result['nickname'] = $_SESSION['wx_user']['nickname'];
            $result['headimgurl'] = $_SESSION['wx_user']['headimgurl'];
            $result['time'] = time();
            $result['voet_num'] = 1;
            $this->add($result);
            M('lord_user')->where(array('lord_id'=>$lord_id))->setInc('scan_num',1);
            M('lord_user')->where(array('lord_id'=>$lord_id))->setInc('voet_num',1);
            M('lord_user')->where(array('lord_id'=>$lord_id))->setInc('fans_num',1);
            $all['create_time'] = date('Y-m-d');
            M('rank')->add($all);
            $this->save_show();
            return 1;
        }else{
            if(intval($info['voet_num']) < 2){
                $this->where($result)->setInc('voet_num',1);
                $this->where($result)->save(array('time'=>time()));
                M('lord_user')->where(array('lord_id'=>$lord_id))->setInc('voet_num',1);
                $all['create_time'] = date('Y-m-d');
                M('rank')->add($all);
                $this->save_show();
                return 1;
            }else{
                $all['create_time'] = date('Y-m-d');
                $voet = M('rank')->where($all)->select();
                if(count($voet)<2){
                    $this->where($result)->setInc('voet_num',1);
                    $this->where($result)->save(array('time'=>time()));
                    M('lord_user')->where(array('lord_id'=>$lord_id))->setInc('voet_num',1);
                    M('rank')->add($all);
                    $this->save_show();
                    return 1;
                }else{
                    $this->where($result)->save(array('time'=>time(),'str'=>M()->getlastsql()));
                    return 0;
                }
            }
        }

    }

    /* 更新排名 */
    public function save_show(){
        $arr = $this->query('SELECT @rownum:=@rownum+1 as num,lord_id AS lord_id FROM (SELECT @rownum:=0) r ,`hm_lord_user` where lord_id<>1 ORDER BY voet_num desc,voet_num desc,scan_num desc;');
        foreach($arr as $k=>$v){
            M('lord_user')->where(array('lord_id'=>$v['lord_id']))->save($v);
        }
    }
}